package com.itany.corejava.code07_面向对象.bms;

import java.util.Scanner;

/**
 * @author: 石小俊
 * @date: 2023年03月13日 9:00
 * @version: 1.0
 */
public class AccountMenu {
    private Scanner sc = new Scanner(System.in);
    // 账户对象
    private Account account;

    // 构建一个有参构造函数,要求实例化该对象,必须传递账户对象
    public AccountMenu(Account account) {
        this.account = account;
    }

    // 显示银行主菜单
    public void showMainMenu() {
        System.out.println("****************欢迎进入银行系统主菜单****************");
        System.out.print("请选择服务(1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还款 7.查询贷款总额 8.修改密码 9.注销):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                // 存款
                deposit();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                queryBalance();
                break;
            case 4:
                transfer();
                break;
            case 5:
                provideLoan();
                break;
            case 6:
                payLoan();
                break;
            case 7:
                queryLoan();
                break;
            case 8:
                modifyPassword();
                break;
            case 9:
                logout();
                break;
        }
    }

    // 存款
    public void deposit() {
        System.out.println("****************开始存款****************");
        System.out.print("请输入存款的金额:");
        double money = sc.nextDouble();
        account.deposit(money);
        System.out.println("提示:存款成功!");
        showMainMenu();
    }

    // 取款
    public void withdraw() {
        System.out.println("****************开始取款****************");
        System.out.print("请输入存款的金额:");
        double money = sc.nextDouble();
        // 对于取款,是根据子类不同其实现方式不同
        // 但是所有账户的父类均为Account
        // 相当于使用多态,子类的对象指向父类的引用
        // 返回值用于判断是否取款成功
        boolean flag = account.withdrawMoney(money);
        if (flag) {
            System.out.println("提示:取款成功!");
        } else {
            System.out.println("提示:余额不足,取款失败!");
        }
        showMainMenu();
    }

    public void queryBalance() {
        System.out.println("****************查询余额****************");
        System.out.println("您账户的余额为:" + account.getBalance() + "元");
        showMainMenu();
    }

    public void transfer() {
        System.out.println("****************开始转账****************");
        System.out.print("请输入要转入的目标账户的账户编号:");
        int id = sc.nextInt();
        // 先查询出对应的目标账户,看是否存在
        Account targetAccount = Bank.findById(id);
        // 如果其值为null,说明账户不存在
        if (targetAccount == null) {
            System.out.println("提示:目标账户不存在");
        } else {
            System.out.print("请输入转账金额:");
            double money = sc.nextDouble();
            // 账户余额大于等于需要转账的金额
            if (account.getBalance() >= money) {
                // 当前账户余额减少money
                account.setBalance(account.getBalance() - money);
                // 目标账户余额增加money
                targetAccount.setBalance(targetAccount.getBalance() + money);
                System.out.println("提示:转账成功,您当前账户余额为:" + account.getBalance());
            } else {
                System.out.println("提示:转账失败,您的账户余额不足,当前余额为:" + account.getBalance());
            }
        }
        showMainMenu();
    }

    // 贷款
    public void provideLoan() {
        System.out.println("****************开始贷款****************");
        // 只有账户为贷款账户时,才能操作贷款功能
        // 由于所有的贷款账户均实现了贷款接口,因此可以直接通过接口进行判断
        if (account instanceof Loanable) {
            System.out.print("请输入贷款金额:");
            double money = sc.nextDouble();
            // 贷款相关功能只有对应的子类存在,父类Account不存在
            // 因此,必须将其转换成接口
            // 子类的对象指向接口的引用
            Loanable loan = (Loanable) account;
            // 执行贷款方法,进行贷款
            loan.provideLoan(money);
            System.out.println("提示:贷款成功,您当前贷款总额为:" + loan.queryLoan());
        } else {
            System.out.println("提示:当前账户不是贷款账户,无法操作贷款功能!");
        }
        showMainMenu();
    }

    // 还贷
    public void payLoan() {
        System.out.println("****************开始还贷****************");
        // 只有账户为贷款账户时,才能操作还贷功能
        if (account instanceof Loanable) {
            Loanable loan = (Loanable) account;
            System.out.print("请输入还贷金额:");
            double money = sc.nextDouble();
            if (loan.payLoan(money)) {
                System.out.println("提示:还贷成功,当前账户余额:" + account.getBalance() + ",贷款总额:" + loan.queryLoan());
            } else {
                System.out.println("提示:账户余额不足,还贷失败!");
            }
        } else {
            System.out.println("提示:当前账户不是贷款账户,无法操作还贷功能!");
        }
        showMainMenu();
    }

    // 查询贷款总额
    public void queryLoan() {
        if (account instanceof Loanable) {
            Loanable loan = (Loanable) account;
            System.out.println("提示:您当前贷款总额为:" + loan.queryLoan());
        }else{
            System.out.println("提示:当前账户不是贷款账户,没有贷款功能!");
        }
        showMainMenu();
    }

    // 修改密码
    public void modifyPassword() {
        System.out.println("****************修改密码****************");
        System.out.print("请输入原密码:");
        String oldPassword = sc.next();
        // 只有原密码正确才能进行修改
        if (oldPassword.equals(account.getPassword())) {
            System.out.print("请输入新密码:");
            String password = sc.next();
            System.out.print("请再次输入新密码:");
            String rePassword = sc.next();
            // 判断两次输入的密码是否一致
            if (password.equals(rePassword)) {
                // 修改当前账户密码
                account.setPassword(password);
                System.out.println("提示:修改成功,请重新登录!");
                new Start().showMenu();
            } else {
                System.out.println("提示:两次输入的密码不一致,修改失败!");
            }
        } else {
            System.out.println("提示:原密码不正确,无法修改!");
        }
        showMainMenu();
    }

    // 注销
    public void logout() {
        System.out.println("提示:注销成功!");
        new Start().showMenu();
    }
}
